//
//  PictureModel.swift
//  SwiftChatUI
//
//  Created by xzk on 2019/2/27.
//  Copyright © 2019年 xzk. All rights reserved.
//

import Foundation
import Photos

let ThumbnailImageWH: CGFloat = (UIScreen.main.bounds.size.width - 3 * 5) / 4

struct PictureModel {
    var asset: PHAsset?
    
    func thumbnail() -> UIImage? {
        var image: UIImage? = nil
        if let asset = asset {
            
            let manager = PHCachingImageManager.default()   // 利用缓存技术 https://blog.csdn.net/mo_xiao_mo/article/details/52612302
            let opt = PHImageRequestOptions()
            opt.isSynchronous = true    // 这个地方如果异步的话会返回模糊图片 ！关键 140M~
            opt.resizeMode = .fast  // ?????????????????
            
            let scale = UIScreen.main.scale
            
            manager.requestImage(for: asset, targetSize: CGSize(width: ThumbnailImageWH * scale,height:ThumbnailImageWH * scale), contentMode: .aspectFill, options: opt) { (thumbnail, info) in
                image = thumbnail
            }
        }
        
        return image
    }
}
